package com.moive.news.tqnewsapplication.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;

import com.google.gson.Gson;
import com.moive.news.tqnewsapplication.R;
import com.moive.news.tqnewsapplication.adapter.CollectAdapter;
import com.moive.news.tqnewsapplication.base.BaseActivity;
import com.moive.news.tqnewsapplication.entity.CollectInfo;
import com.moive.news.tqnewsapplication.networks.BaseApi;
import com.moive.news.tqnewsapplication.okmanager.OkManagerCallback;
import com.moive.news.tqnewsapplication.utils.GetSystemTimeUitl;
import com.moive.news.tqnewsapplication.utils.LogUtil;
import com.moive.news.tqnewsapplication.utils.PreferenceUtil;
import com.moive.news.tqnewsapplication.utils.ToastUtils;
import com.moive.news.tqnewsapplication.weight.mylistview.SecondXListView;
import com.zhy.http.okhttp.OkHttpUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * Created by Administrator on 2017/12/22.
 */

public class CollectActivity extends BaseActivity implements SecondXListView.IXListViewListener {
    @BindView(R.id.collect_back)
    ImageView collectBack;
    @BindView(R.id.pub_all_listview)
    SecondXListView pubAllListview;
    private CollectAdapter adapter;
  private List<CollectInfo.DataBean> list=new ArrayList<>();
    private Gson gson = new Gson();
    private int page = 1;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_collect);
        ButterKnife.bind(this);
        init();
    }
    private void init() {
        collectBack.setOnClickListener(v -> finish());
        adapter = new CollectAdapter(CollectActivity.this);
        pubAllListview.setAdapter(adapter);
        pubAllListview.setXListViewListener(this);
        pubAllListview.setPullLoadEnable(true);
        getdata();
    }
    private void getdata() {
        OkHttpUtils.get()
                .url(BaseApi.COLLECT_LIST)
                .addParams("token", PreferenceUtil.getString("token",""))
                .addParams("page",page+"")
                .addParams("rows", "10")
                .build().execute(new OkManagerCallback(this) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                LogUtil.e(TAG,e.toString());
            }
            @Override
            public void onResponse(String response, int id) {
                super.onResponse(response, id);
                LogUtil.i(TAG,response.toString());
                try {
                    JSONObject jsonObject=new JSONObject(response);
                    JSONArray j2 = jsonObject.getJSONArray("data");
                    if (j2!=null||j2.length()!=0){
                        for (int i = 0; i < j2.length(); i++) {
                            JSONObject j3 = (JSONObject) j2.opt(i);
                           CollectInfo.DataBean bean = gson.fromJson(j3 + "", CollectInfo.DataBean.class);
                            list.add(bean);
                        }
                        adapter.addItems(list);
                    }if (j2.length()==0){
                        ToastUtils.ShortToast("没有更多数据了！");
                    }
                    pubAllListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Intent i = new Intent(CollectActivity.this, NewsDteilsAcitivity.class);
                            Bundle b = new Bundle();
                            b.putString("url", list.get(position-1).getNewsId());
                            LogUtil.i("lidaming",BaseApi.NEWS_TYPE_ITEM_TYPE+ list.get(position-1).getNewsId());
                            i.putExtras(b);
                            startActivity(i);
                        }
                    });
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void onRefresh() {
        page = 1;
        list.clear();
        getdata();
        adapter.notifyDataSetChanged();
        stopOnLoad();
    }
    @Override
    public void onLoadMore() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                page++;
                getdata();
                adapter.notifyDataSetChanged();
                stopOnLoad();
            }
        }, 1000);
    }
    private void stopOnLoad() {
        pubAllListview.stopRefresh();
        pubAllListview.stopLoadMore();
        pubAllListview.setRefreshTime(GetSystemTimeUitl.getTime());
    }
}
